﻿using System;
using System.Collections.Generic;
using System.Linq;


namespace Sustainalytics.ESGRatings.BusinessLogic.Computing
{
    public class ESGComputeEngine
    {
        ESGComputeManager manager;
        bool isStateValid;
        object lockObj = new object();


        protected ESGComputeEngine()
        {
            manager = new ESGComputeManager(new Entities.CompanyEntity[] {});
        }

        public ESGComputeResult ComputeRatings(Guid matrixId, Guid refUnivId, GroupingOptionsEnum groupBy, ComputeOptionsFlags computeOptions)
        {
            lock(lockObj) {
                if(!isStateValid)
                    throw new Exception("invalid state");
                
                var flags = Entities.CompanyAttributesEnum.Private | Entities.CompanyAttributesEnum.TypeA | Entities.CompanyAttributesEnum.Public
                    | Entities.CompanyAttributesEnum.TypeB;

                return manager.ComputeRatings(matrixId, refUnivId, flags, groupBy, computeOptions);
            }
        }


        public List<Guid> GetMatchingCompanies(string peerGroup, string industryGroup)
        {
            return this.manager.GetMatchingCompanies(peerGroup, industryGroup).ToList();
        }

        static ESGComputeEngine _singleton;
        static ESGComputeEngine()
        {
            _singleton = new ESGComputeEngine();
        }

        public static ESGComputeEngine Singleton()
        {
            return _singleton;
        }
    }
}
